Certified Java Programmer Mock Exam


Question 1


class A {
  void m1() {System.out.print("A.m1");}
}
class B extends A {
  void m1() {System.out.print("B.m1");}
  static void m1(String s) {System.out.print(s+",");}
}
class C {
  public static void main (String[] args) {
    B.m1("main");
    new B().m1();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: main,B.m1
b. Compiler Error
c. Runtime Error
d. None of the Above

Question 2


interface A {
  int i = 1;
  int m1();
}
interface B extends A {
  int i = 10;
  int m1();
}
class C implements B {
  public int m1() {return ++i;}
  public static void main(String[] args) {
    System.out.print(new C().m1());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 2
b. Prints: 11
c. Compile-time error.
d. Run-time error.
e. None of the above.

Question 3



Which of the following are modifiers that can be applied to a field declaration within an interface?

a. abstract
b. const
c. final
d. private
e. protected
f. public
g. None of the above.

Question 4


interface A {
  String s1 = "A";
  String m1();        
}
interface B implements A {
  String s1 = "B";
  String m1();             
}
class C implements B {
  public String m1() {return s1;}
  public static void main(String[] args) {
    A a = new C();
    System.out.print(a.m1());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: A
b. Prints: B
c. Compile-time error.
d. Run-time error.
e. None of the above.

Question 5


class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
  static void m1(A x) {System.out.print(x.s1);}
  static void m1(B x) {System.out.print(x.s1);}
  static void m1(C x) {System.out.print(x.s1);}
  public static void main(String[] args) {
    A a; B b; C c;
    a = b = c = new C();
    m1(a);m1(b);m1(c);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CBA
d. Prints: CCC
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 6


class E {
  void printS1(){System.out.print("E.printS1 ");}
  static void printS2() {System.out.print("E.printS2");}
}

class F extends E {
  void printS1(){System.out.print("F.printS1 ");}
  static void printS2() {System.out.print("F.printS2");}
  public static void main (String args[]) {
    E x = new F();
    x.printS1(); x.printS2();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: E.printS1 E.printS2
b. Prints: E.printS1 F.printS2
c. Prints: F.printS1 E.printS2
d. Prints: F.printS1 F.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above

Question 7


class A {static void m() {System.out.print("A");}}
class B extends A {static void m() {System.out.print("B");}}
class C extends B {static void m() {System.out.print("C");}}
class D {
  public static void main(String[] args) {
    C c = new C();
    c.m();
    B b = c;
    b.m();
    A a = b;
    a.m();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CBA
d. Prints: CCC
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 8


class A {
  void m1(A a) {System.out.print("A");}
}
class B extends A {
  void m1(B b) {System.out.print("B");}
}
class C extends B {
  void m1(C c) {System.out.print("C");}
}
class D {
  public static void main(String[] args) {
    A a1 = new A();
    B b1 = new B();
    C c1 = new C();
    A c2 = new C();
    c2.m1(a1);
    c2.m1(b1);
    c2.m1(c1);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 9


 1. interface I1 {}
 2. interface I2 {}
 3. class Base implements I1 {}
 4. class Sub extends Base implements I2 {}
 5. class Yellow {
 6.   public static void main(String args[]) {
 7.     Base base = new Sub();
 8.     I1 i1 = base;
 9.     Sub sub = (Sub)base;
10.     I2 i2 = (Sub)base;
11.   }
12. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 10


class A {}
class B {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  public static void main(String[] args) {
    m(null);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 11


class A {}
class B extends A {}
class C extends B {
  static void m(A x, A y) {System.out.print("AA");}
  static void m(A x, B y) {System.out.print("AB");}
  static void m(B x, A y) {System.out.print("BA");}
  static void m(B x, B y) {System.out.print("BB");}
  public static void main(String[] args) {
    C c = new C();
    m(c, c);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 12


interface Z {void m1();}  // 1
class D implements Z {public final void m1() {}} // 2
class E implements Z {public synchronized void m1() {}} // 3
class F implements Z {public strictfp void m1() {}} // 4
class G implements Z {public native void m1();} // 5

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. None of the above.

Question 13


class A {
  void m1(A a) {System.out.print("A");}
}
class B extends A {
  void m1(B b) {System.out.print("B");}
}
class C extends B {
  void m1(C c) {System.out.print("C");}
}
class D {
  public static void main(String[] args) {
    A c1 = new C();
    B c2 = new C();
    C c3 = new C();
    C c4 = new C();
    c4.m1(c1);
    c4.m1(c2);
    c4.m1(c3);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

Question 14


class Leg{}
class Fur{}
abstract class Pet {
  public abstract void eat();
  public abstract void sleep();
}
class Dog extends Pet {
  Leg leftFront = new Leg();
  Leg rightFront = new Leg();
  Leg leftRear = new Leg();
  Leg rightRear = new Leg();
  Fur fur = new Fur();
  public Fur shed() {return fur;}
  public void eat() {}
  public void sleep() {}
}
class Cat extends Dog {
  public void ignoreOwner() {}
  public void climbTree() {}
}

Which of the following are true statements?

a. A Cat object inherits Fur and four Legs from the Dog super class.
b. A Cat object is able to sleep and eat.
c. A Cat object is able to climb a tree.
d. The relationship between Dog and Pet is an example of an appropriate use of inheritance.
e. The relationship between Cat and Dog is an example of an appropriate use of inheritance.
f. None of the Above.

Question 15


class L {
  static String m(float i) {return "float";}
  static String m(double i) {return "double";}
  public static void main (String[] args) {
    char a = 1;
    long b = 2;
    System.out.print(m(a)+","+ m(b));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 16


1.  interface I1 {}
2.  interface I2 {}
3.  class Base implements I1 {}
4.  class Sub extends Base implements I2 {}
5.  class Gray {
6.    public static void main(String []args) {
7.      Base[] base = {new Base()};
8.      Sub sub[] = {new Sub()};
9.      Object obj = sub;
10.     base = obj;
11.   }
12. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 17


class A {}
class B {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  static void m(A x) {System.out.print("A");}
  public static void main(String[] args) {
    m(null);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1